package com.bgs.eat.userProtal.service.impl;

import com.bgs.eat.config.CommonResult;
import com.bgs.eat.fileManager.service.FileService;
import com.bgs.eat.page.MyPageHelper;
import com.bgs.eat.page.PageRequest;
import com.bgs.eat.page.PageResult;
import com.bgs.eat.userProtal.entity.UserProtalEntity;
import com.bgs.eat.userProtal.mapper.UserProtalMapper;
import com.bgs.eat.userProtal.service.UserProtalService;
import com.bgs.eat.utils.MailUtils;
import com.bgs.eat.utils.RanNum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName UserServiceImpl
 * @Description TODO
 * @Author hbx
 * @Date 2022/10/11 14:44
 **/
@Service
public class UserProtalServiceImpl implements UserProtalService {
    @Autowired
    UserProtalMapper userProtalMapper;

    @Autowired
    FileService fileService;
    /**
    * @Author hbx
    * @Description 新增用户门户 （用户登录）
    * @Date 2022/10/11 14:57
    * @param userName
    * @param password
    */
    @Override
    public int addUserProtal(String userName, String password) {
        UserProtalEntity userProtalEntity = new UserProtalEntity();
        userProtalEntity.setUserName(userName);
        String md5Str = DigestUtils.md5DigestAsHex(password.getBytes());
        userProtalEntity.setPassword(md5Str);
        userProtalEntity.setDelFlag(0);
        userProtalEntity.setCreateTime(new Date());
        userProtalEntity.setRole(1);
        userProtalEntity.setAuditId(0);
        userProtalEntity.setAvatarId(116);
        userProtalEntity.setNickName(RanNum.randomName(true,4));
        return userProtalMapper.insertSelective(userProtalEntity);
    }

    /**
     * @Author hbx
     * @Description 根据审批id查询用户信息 (后端审批管理)
     * @Date 2022/10/14 11:22
     * @param pageRequest
     * @param userName
     */
    @Override
    public PageResult getUserProtalByAudit(PageRequest pageRequest, String userName) {
       if (userName == ""){
           MyPageHelper.startPage(pageRequest);
           Example example = new Example(UserProtalEntity.class);
           example.createCriteria().andEqualTo("delFlag",0).andEqualTo("auditId",1);
           PageResult pageResult = MyPageHelper.pageInfo(userProtalMapper.selectByExample(example));
           return pageResult;
       } else {
           MyPageHelper.startPage(pageRequest);
           Example example = new Example(UserProtalEntity.class);
           example.createCriteria().andLike("userName","%"+userName+"%").andEqualTo("delFlag",0).andEqualTo("auditId",1);
           PageResult pageResult = MyPageHelper.pageInfo(userProtalMapper.selectByExample(example));
           return pageResult;
       }
    }

    /**
    * @Author hbx
    * @Description 修改用户账号信息
    * @Date 2022/10/17 14:06
    * @param id
    * @param userName
    */
    @Override
    public Integer updateAccount(Integer id, String userName) {
        Example example = new Example(UserProtalEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",id);
        UserProtalEntity userProtal = new UserProtalEntity();
        UserProtalEntity userProtals = userProtalMapper.selectOneByExample(example);
        BeanUtils.copyProperties(userProtals,userProtal);
        userProtal.setUserName(userName);
        return userProtalMapper.updateByExample(userProtal,example);
    }

    /**
    * @Author hbx
    * @Description 修改用户昵称
    * @Date 2022/10/17 14:40
    * @param id
    * @param nickName
    */
    @Override
    public Integer updateNickName(Integer id, String nickName) {
        Example example = new Example(UserProtalEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",id);
        UserProtalEntity userProtal = new UserProtalEntity();
        UserProtalEntity userProtals = userProtalMapper.selectOneByExample(example);
        BeanUtils.copyProperties(userProtals,userProtal);
        userProtal.setNickName(nickName);
        return userProtalMapper.updateByExample(userProtal,example);
    }

    /**
    * @Author hbx
    * @Description 修改自我介绍
    * @Date 2022/10/17 14:51
    * @param id
    * @param introduction
    */
    @Override
    public Integer updateIntroduction(Integer id, String introduction) {
        Example example = new Example(UserProtalEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",id);
        UserProtalEntity userProtal = new UserProtalEntity();
        UserProtalEntity userProtals = userProtalMapper.selectOneByExample(example);
        BeanUtils.copyProperties(userProtals,userProtal);
        userProtal.setIntroduction(introduction);
        return userProtalMapper.updateByExample(userProtal,example);
    }

    /**
    * @Author cjl
    * @Description 审核骑手成功 发送邮箱提示
    * @Date  10:19
    * @Return int
    * @param userProtalEntity
    */
    @Override
    public int updateHorsemanauDitIdSucceed(UserProtalEntity userProtalEntity) {
        int i = 0;
//        if (userProtalEntity.getMail().equals("")){
//        } else {
//            String mail = userProtalEntity.getMail();
//            MailUtils.sendMail(mail,"吃了么","您的骑手申请已通过");
//        }
//        System.out.println(userProtalEntity.getMail().equals("null"));
        if (!userProtalEntity.getMail().equals("") && !userProtalEntity.getMail().equals("null") && userProtalEntity.getMail() != null){
            String mail = userProtalEntity.getMail();
            MailUtils.sendMail(mail,"吃了么","您的骑手申请已通过");
        }
        userProtalEntity.setAuditId(5);
        userProtalEntity.setRole(3);
        i = userProtalMapper.updateByPrimaryKeySelective(userProtalEntity);
        return i;
    }

    /**
    * @Author cjl
    * @Description 审核骑手失败 发送邮箱提示
    * @Date  10:20
    * @Return int
    * @param map
    */
    @Override
    public int updateHorsemanauDitIdFail(Map<String,String> map) {
        String id = map.get("id");
        String mail = map.get("mail");
        String reason = map.get("reason");
        UserProtalEntity userProtalEntity = new UserProtalEntity();
        int i = 0;
        if (mail.equals("")){
        } else {
            MailUtils.sendMail(mail,"吃了么","您的骑手申请已驳回"+"<br>"+"驳回理由:"+reason);
        }
        userProtalEntity.setId(Integer.parseInt(id));
        userProtalEntity.setAuditId(3);
        i = userProtalMapper.updateByPrimaryKeySelective(userProtalEntity);
        return i;
    }

    /**
    * @Author cjl
    * @Description 审核商家成功 发送邮箱提示
    * @Date  10:20
    * @Return int
    * @param userProtalEntity
    */
    @Override
    public int updateMerchantDitIdSucceed(UserProtalEntity userProtalEntity) {
        int i = 0;
        System.out.println(userProtalEntity.getMail().equals("null"));
        if (!userProtalEntity.getMail().equals("") && !userProtalEntity.getMail().equals("null") && userProtalEntity.getMail() != null){
            String mail = userProtalEntity.getMail();
            MailUtils.sendMail(mail,"吃了么","您的店铺申请已通过");
        }
        userProtalEntity.setAuditId(5);
        userProtalEntity.setRole(2);
        i = userProtalMapper.updateByPrimaryKeySelective(userProtalEntity);
        return i;
    }

    /**
    * @Author cjl
    * @Description 审核商家失败 发送邮箱提示
    * @Date  10:20
    * @Return int
    * @param map
    */
    @Override
    public int updateMerchantDitIdFail(Map<String,String> map) {
        String id = map.get("id");
        String mail = map.get("mail");
        String reason = map.get("reason");
        UserProtalEntity userProtalEntity = new UserProtalEntity();
        int i = 0;
        if (mail.equals("")){
        } else {
            MailUtils.sendMail(mail,"吃了么","您的店铺申请已驳回"+"<br>"+"驳回理由:"+reason);
        }
        userProtalEntity.setId(Integer.parseInt(id));
        userProtalEntity.setAuditId(4);
        i = userProtalMapper.updateByPrimaryKeySelective(userProtalEntity);
        return i;
    }


    /**
    * @Author cjl
    * @Description 根据审批id查询用户信息 (后端审批管理) 商家审批管理
    * @Date  10:24
    * @Return com.bgs.eat.page.PageResult
    * @param pageRequest
    * @param userName
    */
    @Override
    public PageResult getUserProtalByAuditMerchant(PageRequest pageRequest, String userName) {
        if (userName == ""){
            MyPageHelper.startPage(pageRequest);
            Example example = new Example(UserProtalEntity.class);
            example.createCriteria().andEqualTo("delFlag",0).andEqualTo("auditId",2);
            PageResult pageResult = MyPageHelper.pageInfo(userProtalMapper.selectByExample(example));
            return pageResult;
        } else {
            MyPageHelper.startPage(pageRequest);
            Example example = new Example(UserProtalEntity.class);
            example.createCriteria().andLike("userName","%"+userName+"%").andEqualTo("delFlag",0).andEqualTo("auditId",2);
            PageResult pageResult = MyPageHelper.pageInfo(userProtalMapper.selectByExample(example));
            return pageResult;
        }
    }

    /**
    * @Author cjl
    * @Description 删除审核的用户
    * @Date  11:25
    * @Return java.lang.Integer
    * @param id
    */
    @Override
    public Integer deleteUserProtalById(Integer id) {
        Example example = new Example(UserProtalEntity.class);
        example.createCriteria().andEqualTo("id", id);
        UserProtalEntity userProtalEntity = new UserProtalEntity();
        userProtalEntity.setDelFlag(1);
        int i = userProtalMapper.updateByExampleSelective(userProtalEntity, example);
        return i;
    }
    /**
    * @Author hbx
    * @Description 修改用户个人头像
    * @Date 2022/10/18 8:31
    * @param file
    */
    @Override
    public CommonResult applyUploadImage(MultipartFile file) {
        Integer imageId = fileService.uniUploadFile(file);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("imageId", imageId);
        return CommonResult.ok(map);
    }

    /**
    * @Author hbx
    * @Description 修改用户头像 （前端）
    * @Date 2022/10/18 8:46
    * @param id
    * @param avatarId
    */
    @Override
    public Integer updateAvatarId(Integer id, Integer avatarId) {
        Example example = new Example(UserProtalEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",id);
        UserProtalEntity userProtal = new UserProtalEntity();
        UserProtalEntity userProtals = userProtalMapper.selectOneByExample(example);
        BeanUtils.copyProperties(userProtals,userProtal);
        userProtal.setAvatarId(avatarId);
        return userProtalMapper.updateByExample(userProtal,example);
    }

    /**
    * @Author hbx
    * @Description 根据id查询用户信息
    * @Date 2022/10/18 15:11
    * @param id
    */
    @Override
    public CommonResult getAllUserById(Integer id) {
        Example example = new Example(UserProtalEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",id);
        UserProtalEntity userProtals = userProtalMapper.selectOneByExample(example);
        return CommonResult.ok(userProtals);
    }

    /**
    * @Author hbx
    * @Description 根据用户id修改手机号
    * @Date 2022/10/18 20:38
    * @param id
    * @param phone
    */
    @Override
    public Integer updatePhoneById(Integer id, String phone) {
        Example example = new Example(UserProtalEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",id);
        UserProtalEntity userProtal = new UserProtalEntity();
        UserProtalEntity userProtals = userProtalMapper.selectOneByExample(example);
        BeanUtils.copyProperties(userProtals,userProtal);
        userProtal.setPhone(phone);
        return userProtalMapper.updateByExample(userProtal,example);
    }
    /**
    * @Author cjl
    * @Description 全查 前端用户（模糊查用户昵称）
    * @Date  10:53
    * @Return com.bgs.eat.page.PageResult
    * @param pageRequest
    * @param userName
    */
    @Override
    public PageResult getUserProtalByRoleOne(PageRequest pageRequest, String userName) {
        MyPageHelper.startPage(pageRequest);
        Example example = new Example(UserProtalEntity.class);
        example.createCriteria().andLike("userName","%"+userName+"%").andEqualTo("delFlag",0).andEqualTo("role",1);
        PageResult pageResult = MyPageHelper.pageInfo(userProtalMapper.selectByExample(example));
        return pageResult;
    }

    /**
    * @Author cjl
    * @Description 全查 前端商家（模糊查用户昵称）
    * @Date  14:19
    * @Return com.bgs.eat.page.PageResult
    * @param pageRequest
    * @param userName
    */
    @Override
    public PageResult getUserProtalByRoleTwo(PageRequest pageRequest, String userName) {
//        if (roleId == 1){
            MyPageHelper.startPage(pageRequest);
            Example example = new Example(UserProtalEntity.class);
            example.createCriteria().andLike("userName","%"+userName+"%").andEqualTo("delFlag",0).andEqualTo("role",2);
            PageResult pageResult = MyPageHelper.pageInfo(userProtalMapper.selectByExample(example));
            return pageResult;

    }

    /**
    * @Author cjl
    * @Description 全查 前端骑手（模糊查用户昵称）
    * @Date  14:19
    * @Return com.bgs.eat.page.PageResult
    * @param pageRequest
    * @param userName
    */
    @Override
    public PageResult getUserProtalByRoleThree(PageRequest pageRequest, String userName,Integer countyId, Integer roleId) {
        if (roleId == 6){
            MyPageHelper.startPage(pageRequest);
            PageResult pageResult = MyPageHelper.pageInfo(userProtalMapper.selectRegionByUserProtal(userName,countyId,roleId));
            return pageResult;
        } else {
            MyPageHelper.startPage(pageRequest);
            Example example = new Example(UserProtalEntity.class);
            example.createCriteria().andLike("userName","%"+userName+"%").andEqualTo("delFlag",0).andEqualTo("role",3);
            PageResult pageResult = MyPageHelper.pageInfo(userProtalMapper.selectByExample(example));
            return pageResult;
        }
    }

    /**
    * @Author cjl
    * @Description 修改 前端（用户，商家，骑手）
    * @Date  14:19
    * @Return int
    * @param userProtalEntity
    */
    @Override
    public int updateUserProtalEntity(UserProtalEntity userProtalEntity) {
        int i = 0;
        i = userProtalMapper.updateByPrimaryKeySelective(userProtalEntity);
        return i;
    }


}
